package com.ds.ui {
	import com.ds.factory.TFFactory;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	/**
	 * @author doctorstal
	 * Serves for easy creating buttons. 
	 */
	public class LabeledBtn extends Sprite {
		protected var _label : String;
		protected var _bgColor : Number = 0xcccccc;
		
		public var _tf:TextField;
		public var _bg : Shape;
		private var _padding : Number = 10;
		
		public var userData:*;
		
		
		
		public function LabeledBtn(label:String=null,backgroundColor:uint=0xcccccc,userData:*=null) {
			buttonMode=true;
			useHandCursor=true;
			mouseChildren=false;
			
			this._bgColor=backgroundColor;
			this.userData=userData;
			if(label){
				this.label=label;
			}
		}
		
		public function get label():String{
			return _label;
		}
		public function set label(s:String):void{
			_label=s;
			initLabel();
		}
		
		public function get bgColor() : Number {
			return _bgColor;
		}

		public function set bgColor(bgColor : Number) : void {
			_bgColor = bgColor;
			drawBg();
		}

		private function initLabel() : void {
			if(!_tf){
				_tf=TFFactory.createTextField();
				_tf.x=_padding/2;
				_tf.y=_padding/2;
				
				addChild(_tf);
			}
			_tf.htmlText=_label;
			drawBg();
		}

		private function drawBg() : void {
			if(_bg&&contains(_bg)){
				removeChild(_bg);
			}
			if(_tf is TextField){
				var b:Rectangle=_tf.getBounds(this);
				b.inflate(_padding/2,_padding/2);
				_bg=new Shape();
				addChildAt(_bg,0);
				_bg.graphics.beginFill(_bgColor);
				_bg.graphics.drawRoundRect(b.x,b.y,b.width,b.height,5);
				_bg.graphics.endFill();
				
				addEventListener(MouseEvent.MOUSE_OVER,bgOverHandler);
				addEventListener(MouseEvent.MOUSE_OUT,bgOutHandler);
			}
		}

		private function bgOutHandler(event : MouseEvent) : void {
			if(_bg){
				_bg.filters=[];
				
			}
		}

		private function bgOverHandler(event : MouseEvent) : void {
			if(_bg){
				_bg.filters=[new GlowFilter(_bgColor,1,5,5,2,2)];
			}
		}

		
	}
}
